|
|
|
|
|
|
|
loop. In the preceding code, a variable is declared as a Form data type. Each time through the loop, the next form pointer in the collection is returned and assigned to the frmAny variable. Another advantage of the For...Each construct is that you do not need to know how many items there are in the collection. |
|
|
|
|
|
|
|
|
Keeping Track of Form Instances |
|
|
|
|
|
|
|
|
If you use the technique of creating forms using the New keyword, youll need to start keeping track of which forms are open. For example, if you create multiple instances of an employee form, then how do you know which form contains which employee? |
|
|
|
|
|
|
|
|
Whats more, you cannot reference any of these created forms by their names, because they all have the same name. You need to come up with a method for referencing each individual form. One way is to use the Forms collection. |
|
|
|
|
|
|
|
|
Examples Filenames: EmpList.frm and Employee.frm |
|
|
|
|
|
|
|
|
In the sample project for this chapter, youll find two forms that allow you to select an employee from a list and then display that employees details in an employee form. You can keep that employees form open while you click yet another employee in the list and display that employees information in another instance of the Employee Information form. When you start to open multiple instances of forms, youll need a method of tracking which forms are open. In addition, youll need to detect whether youve already opened a detail form for an employee. If you have that detail form open, you dont want to open another oneyou just want to select it. |
|
|
|
|
|
|
|
|
Lets look at a method for keeping track of which form goes with which employee. When you open a new Employee Information form, youll store the employee ID into the Tag property of the employee form. Heres some code from the Edit button on the EmpList.frm form: |
|
|
|
 |
|
|
|
|
Private Sub cmdEdit_Click()
Dim frmNew As frmEmployee
Dim lngEmpID As Lo |
|
|
|
 |
|
|
|
|
If lstNames.ListIndex <> -1 Then lngEmpID = lstNames.ItemData(lstNames.ListIndex) If Not FormFind(lngEmpID) Then
' Create new instance of the form Set frmNew = New frmEmployee |
|
|
|
|
|